﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PdfLibrary
{
    public abstract class PdfDictionaryObject
    {
        public PdfDictionary BaseDictionary;

        public PdfDictionaryObject(PdfDictionary baseDictionary)
        {
            this.BaseDictionary = baseDictionary;
        }

        public T GetValue<T>(string name) where T : IPdfObject
        {
            IPdfObject result = null;
            this.BaseDictionary.TryGetValue(name, out result);
            return (T)result;
        }

        public T GetRequiredValue<T>(string name, Exception exception = null) where T : IPdfObject
        {
            var obj = GetValue<T>(name);
            if (obj == null)
                throw exception ?? new Exception(String.Format("Missing field: {0}", name));
            return obj;
        }

        public double GetNumber(string name, double defaultValue = default(double))
        {
            var obj = GetValue<PdfNumber>(name);
            return obj != null ? obj.value : defaultValue;
        }
    }
}
